const PointCodes = new Set();

/**
 * 记录生成的pointCode
 */
export const recordPointCode = (code: string) => {
  PointCodes.add(code);
};

/****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
const $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
const maxPos = $chars.length;

/**
 * 随机生成指定的任意位数字符串
 * @param {*} len
 */
export const randomString = (prefix: string | null, len = 32): string => {
  const arr = [];
  for (let i = 0; i < len; i++) {
    arr.push($chars.charAt(Math.floor(Math.random() * maxPos)));
  }
  const pwd = (prefix ? prefix + '_' : '') + arr.join('');
  if (PointCodes.has(pwd)) {
    return randomString(prefix, len);
  }
  // 记录码点code
  recordPointCode(pwd);
  return pwd;
};

